package com.example.testanimation.fragment.switchanimation;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.nova.scenic.projectlibs.util.adapter.SimpleAdapter;
import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.nova.scenic.projectlibs.util.fragment.BaseFragment;
import com.scenic.rd_android.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by scenic on 2015/3/26.
 */
public class ListFragment extends BaseFragment {


    private static final String TAG = "ListFragment";
    private GridView mGridView;
    private MyAdapter myAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.list_fragment_layout,null);
        mGridView = (GridView) view.findViewById(R.id.id_list_test);
        return view;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<String> list = new ArrayList<String>();
        for(int i = 0;i < 100; i ++){
            list.add("test " + i);
        }
        myAdapter = new MyAdapter(getActivity(),list);
        mGridView.setAdapter(myAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                int x = (int) view.getX();
                int y = (int) view.getY();
                bundle.putInt("x",x);
                bundle.putInt("y",y);
                bundle.putInt("width",view.getWidth());
                bundle.putInt("height",view.getHeight());
                MyLog.i(TAG, " x " + x + " y " + y);

                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                InformationFragment informationFragment = (InformationFragment) Fragment.instantiate(getActivity(),
                        InformationFragment.class.getName(),bundle);
                transaction.add(android.R.id.content,informationFragment,"infomation");
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });
    }

    class MyAdapter extends SimpleAdapter<String> {

        public MyAdapter(Context context, List<String> list) {
            super(context, list);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.adapter_list,null);
            return view;
        }
    }

}
